﻿#region Copyright Notice
// 
// 
// Copyright (c) 2009-2010 Anindya Chatterjee
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using DigitalRune.Windows.Docking;
using Event.Plugin.Utility;

namespace DesktopNotifier.Utility
{
    public partial class NotifyHost : KryptonForm
    {
        private readonly List<BaseNotifyTip> _children;
        private readonly INotifier _notifier;
        private Point _location;
        private int _y;

        public NotifyHost(INotifier notifier)
        {
            InitializeComponent();
            InitializeAnimation();

            _notifier = notifier;
            _children = new List<BaseNotifyTip>();
        }

        private void InitializeAnimation()
        {
            dockPanel.ShowAutoHideButton = false;
            dockPanel.ShowDocumentIcon = true;
            dockPanel.ShowScrollButtons = false;
            dockPanel.ShowCloseDocumentButton = true;
            dockPanel.ShowScrollButtons = true;
            dockPanel.ShowWindowList = false;
            dockPanel.ShowCloseButton = false;

            ToolStripManager.VisualStylesEnabled = true;
            ToolStripManager.Renderer = new ToolStripOffice2007Renderer(Office2007Theme.Blue);
            DockPanelManager.RenderMode = DockPanelRenderMode.Office2007Blue;
            BackColor = DockPanelManager.BackColor;
            dockPanel.Refresh();

            int x = Screen.PrimaryScreen.WorkingArea.Width - Width;
            int y = Screen.PrimaryScreen.WorkingArea.Height - Height;
            _location = new Point(x, y);
            _y = Screen.PrimaryScreen.WorkingArea.Height;
            TopMost = true;
        }

        private void NotifyHost_Load(object sender, EventArgs e)
        {
            Location = new Point(_location.X, _y);
            timer.Enabled = true;
        }

        private void NotifyHost_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_notifier != null)
                _notifier.StopAnimateIcon();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (_y > _location.Y)
            {
                Location = new Point(_location.X, _y);
                _y = _y - 3;
            }
            else
            {
                Location = _location;
                timer.Enabled = false;
            }
        }

        public void RegisterNotifyTip(BaseNotifyTip notifyTip)
        {
            foreach (BaseNotifyTip child in _children)
            {
                if (child.Event.Id == notifyTip.Event.Id)
                {
                    child.Update(notifyTip);
                    child.Show(dockPanel, DockState.Document);
                    return;
                }
            }
            _children.Add(notifyTip);
            notifyTip.Host = this;
            notifyTip.VisibleChanged += NotifyTipVisibleChanged;
            notifyTip.Show(dockPanel, DockState.Document);
            Show();
        }

        private void NotifyTipVisibleChanged(object sender, EventArgs e)
        {
            var notifyTip = sender as BaseNotifyTip;
            if (notifyTip != null && notifyTip.Visible)
            {
                Icon = notifyTip.TipIcon;
                Text = notifyTip.TipHeading;
            }
        }
    }
}